<?php

namespace app\api\controller;

use app\admin\model\project\ProjectApply;
use app\api\model\project\ProjectCategory;
use app\api\model\project\ProjectMedia;
use app\api\model\project\ProjectStaffTime;
use app\common\controller\Api;
use app\api\model\project\Project as ProjectModel;
use app\api\model\project\ProjectStaff as ProjectStaffModel;
use app\api\model\project\ProjectApply as ProjectApplyModel;
use app\api\model\project\ProjectOrder as ProjectOrderModel;
use app\api\model\project\ProjectEvaluate as ProjectEvaluateModel;
/**
 * 服务接口
 */
class Project extends Api
{
    protected $noNeedLogin = ['index','recommend','lists','info','data','category'];
    protected $noNeedRight = ['*'];

    //服务首页的点赞/转发/评论/收藏数
    public function data()
    {
        $zan_num = ProjectStaffModel::sum('zan_num');
        $collect_num = ProjectStaffModel::sum('collect_num');
        $repost_num = ProjectStaffModel::sum('repost_num');
        $comment_num = ProjectStaffModel::sum('comment_num');

        $data = [
            'zan_num'=>$zan_num,
            'collect_num'=>$collect_num,
            'repost_num'=>$repost_num,
            'comment_num'=>$comment_num
        ];

        $this->success('',$data);
    }

    //首页服务
    public function index()
    {
        $city_id = input('city_id',0);
        $category_list = ProjectCategory::field('id,name')->order('weigh desc')->select();
        foreach ($category_list as $k=>$v){
            $where = ['pa.category_id'=>$v['id'],'pa.status'=>1];
           /* if ($city_id){
                $where['ps.city_id'] = $city_id;
            }*/
            $project = ProjectApplyModel::alias('pa')
                ->join('project_staff ps','pa.staff_id = ps.id AND ps.deletetime IS NULL')
                ->where($where)
                ->field('pa.staff_id,pa.is_recommend,pa.price,pa.unit,pa.name,pa.status,pa.is_recommend,ps.image,ps.nickname,ps.realname,ps.name as title')
                ->limit(6)
                ->order('pa.is_top desc pa.is_recommend desc')
                //->order('ps.zan_num desc')
                ->select();
            $category_list[$k]['project'] = $project;
        }

        $this->success('',$category_list);
    }

    //首页推荐服务
    public function recommend()
    {
        $city_id = input('city_id',0);
        $where = ['pa.is_recommend'=>1,'pa.status'=>1];
        if ($city_id){
            //$where['ps.city_id'] = $city_id;
        }
        $project = ProjectApplyModel::alias('pa')
            ->join('project_staff ps','pa.staff_id = ps.id AND ps.deletetime IS NULL')
            ->where($where)
            ->field('pa.staff_id,pa.is_recommend,pa.price,pa.unit,pa.name,pa.status,pa.is_recommend,ps.image,ps.nickname,ps.realname,ps.name as title')
            ->limit(8)
            ->order('pa.is_top desc pa.is_recommend desc')
            //->order('pa.order_money,ps.zan_num desc')
            ->select();
        $this->success('',$project);
    }

    //服务列表
    public function lists()
    {
        $category_id = input('category_id',0);
        $is_recommend = input('is_recommend',0);
        $keyword = input('keyword','');
        $city_id = input('city_id',0);
        $where = ['pa.status'=>1];
        /*if ($city_id){
            $where['ps.city_id'] = $city_id;
        }*/
        if ($category_id){
            $where['pa.category_id'] = $category_id;
        }
        if ($is_recommend){
            $where['pa.is_recommend'] = 1;
        }
        if(!empty($keyword)){
            $where['pa.name|ps.nickname|ps.name'] = ['like',"%$keyword%"];
        }

        $project = ProjectApplyModel::alias('pa')
            ->join('project_staff ps','pa.staff_id = ps.id AND ps.deletetime IS NULL')
            ->where($where)
            ->field('pa.staff_id,pa.is_recommend,pa.price,pa.unit,pa.name,pa.is_recommend,ps.image,pa.status,ps.nickname,ps.realname,ps.name as title,ps.zan_num')
            //->order('ps.zan_num desc')
            ->order('pa.is_top desc pa.is_recommend desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$project);
    }

    //服务详情
    public function info()
    {
        $staff_id = input('staff_id',0);
        $info = ProjectStaffModel::where(['id'=>$staff_id])->field('id as staff_id,user_id,nickname,realname,gender,age,education,
        major,title,mobile,province,city,district,image,zan_num,collect_num,repost_num,user_level,name,region_limit,self_intro,
        place_province,place_city,place_district')->find();

        $user_model = new \app\api\model\User();
        $user = \app\api\model\User::get($info['user_id']);
        $label = $user_model->getLevelLabelTextAttr($user);
        $info['level_label'] = $label;
        if ($user['company_auth']==1){
            $info['intro'] = $user['company_intro'];
        } else {
            $info['intro'] = $user['personal_intro'];
        }

        if ($info['user_level']=='0'){
            $info['user_level_text'] = '不限';
        } else {
            $user_level_arr = [];
            $user_level = explode(',',$info['user_level']);
            foreach ($user_level as $k=>$v){
                if ($v==1){
                    array_push($user_level_arr,'普享会员');
                } elseif ($v==2){
                    array_push($user_level_arr,'惠享会员');
                } elseif ($v==3){
                    array_push($user_level_arr,'乐享会员');
                } elseif ($v==4){
                    array_push($user_level_arr,'优享会员');
                } elseif ($v==5){
                    array_push($user_level_arr,'尊享会员');
                }
            }
            $info['user_level_text'] = implode('/',$user_level_arr);
        }
        //$info['comment_num'] = ProjectEvaluateModel::where(['staff_id'=>$staff_id])->count();
        $cert_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>0])->limit(4)->select();
        $info['cert_list'] = $cert_list;
        $info['cert_img_count'] = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>0])->count();
        $image_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>1])->limit(4)->select();
        $info['image_list'] = $image_list;
        $info['image_count'] =ProjectMedia::where(['staff_id'=>$staff_id,'type'=>1])->count();
        $video_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>2])->limit(10)->select();
        $info['video_list'] = $video_list;
        $info['video_count'] = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>2])->count();
        $project_apply_list = ProjectApplyModel::where(['staff_id'=>$staff_id])->select();
        $apply_model = new ProjectApply();
        foreach ($project_apply_list as $k=>$v){
            $project_apply_list[$k]['project_name'] = $info?$info['name']:'';
            $category = ProjectCategory::get($v['category_id']);
            $project_apply_list[$k]['category_name'] = $category?$category['name']:'';

            //dump($work_day_arr);die;
            $project_apply_list[$k]['work_day'] = $apply_model->getWorkWeek($v['work_day']);
        }
        $info['project_apply_list'] = $project_apply_list;


        $this->success('',$info);
    }

    //已接订单时间段
    public function ordertime()
    {
        $staff_id = input('staff_id',0);

        $order_list = collection(ProjectOrderModel::where(['staff_id'=>$staff_id])
            ->where('service_start_date','>',date('Y-m-d'))
            ->field('service_start_date,service_end_date,service_start_time,service_end_time')
            ->select())->toArray();

        $time_list = collection(ProjectStaffTime::where(['staff_id'=>$staff_id])
            ->where('service_start_date','>',date('Y-m-d'))
            ->field('service_start_date,service_end_date,service_start_time,service_end_time')
            ->select())->toArray();

        $arr = array_merge($order_list,$time_list);
        array_multisort(array_column($arr, 'service_start_date'), SORT_ASC, $arr);
        $this->success('',$arr);
    }

    //添加不提供服务时间段
    public function addtime()
    {
        $staff_id = input('staff_id',0);

        $time_model = new ProjectStaffTime();

        $data = [
            'staff_id'=>$staff_id,
            //'user_id'=>$this->auth->id,
            'service_start_date'=>input('service_start_date',''),
            'service_end_date'=>input('service_end_date',''),
            'service_start_time'=>input('service_start_time',''),
            'service_end_time'=>input('service_end_time',''),
        ];

        $res = $time_model->save($data);
        $this->success('',$res);
    }

    //增加服务人员的转发次数
    public function addrepost()
    {
        $staff_id = input('staff_id',0);
        ProjectStaffModel::where(['id'=>$staff_id])->setInc('repost_num',1);
        $this->success('');
    }

    //服务人员的资质证书/服务图片/服务视频
    public function media()
    {
        $staff_id = input('staff_id',0);

        $cert_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>0])->select();
        $info['cert_list'] = $cert_list;
        $image_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>1])->select();
        $info['image_list'] = $image_list;
        $video_list = ProjectMedia::where(['staff_id'=>$staff_id,'type'=>2])->select();
        $info['video_list'] = $video_list;

        $this->success('',$info);
    }

    //服务项目
    public function project()
    {

        $category_list = ProjectCategory::field('id,name')->order('weigh desc')->select();
        foreach ($category_list as $k=>$v){
            $category_list[$k]['project'] = ProjectModel::where(['category_id'=>$v['id']])
                ->field('id,category_id,name,unit,remark')->order('weigh desc')->select();
        }

        $this->success('',$category_list);
    }


    //申请服务项目
    public function apply()
    {
        try {
            $model = new ProjectStaffModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->apply($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);
    }

    //分类列表
    public function category()
    {
        $category_list = ProjectCategory::field('id,name,image')->order('weigh desc')->select();
        $this->success('',$category_list);
    }
}